package com.jwyming.secKilling;

import org.apache.log4j.Logger;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by user on 2017/4/4.
 */
public class ConfigInfo {
    Logger logger = Logger.getLogger(this.getClass().getName());

    public List<ConfigItem> getConfigItems() throws Exception {
//        // 载入配置文件
        List<ConfigItem> configItems = new ArrayList<ConfigItem>();

        for(int i = 1; i < 1000; i++) {
            String fileName = "request" + convertNumberToStr(i) + ".properties";
//            logger.info(fileName);
            InputStream inputStream = ConfigInfo.class.getClassLoader().getResourceAsStream(fileName);
            if(inputStream != null) {
                if(inputStream.available() > 0) {
                    configItems.add(new ConfigItem(inputStream));
                }
                inputStream.close();
            }

        }
        return configItems;
    }

    private String convertNumberToStr(int i) {
        if(i <= 0) {
            logger.error("Cannot convert number less than 0");
            return "001";
        } else if(i > 0 && i < 10) {
            return String.valueOf("00" + i);
        } else if(i >= 10 && i < 100) {
            return String.valueOf("0" + i);
        } else if(i >= 100 && i < 1000) {
            return String.valueOf(i);
        } else {
            logger.error("Cannot convert number more than 100");
            return String.valueOf(i % 1000);
        }
    }


}
